#pragma GCC optimize(2, "Ofast", "inline")
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <algorithm>
#define endl '\n'
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define int128 __int128_t
using namespace std;

vector<int> p;
int a, n;
int sum = 0;
bool check(int x)
{
    if(x>n) return 0;
    string ans = "";
    for (int i = x;i;i /= a)
    {
        ans += to_string(i % a);
    }
    string t = ans;
    reverse(t.begin(), t.end());

    return ans == t;
}
// 直接生成1到n之间的所有回文数
void f()
{
    for (int i = 1; ; ++i)
    {
        string s = to_string(i);
        string rev = s;
        reverse(rev.begin(), rev.end());
        int num1 = stoll(s + rev.substr(1)); // 生成偶数位回文数
        if (check(num1))  sum += num1;
        int num2 = stoll(s + rev); // 生成奇数位回文数
        if (check(num2))  sum += num2;
        if(num1 > n && num2 > n) break;
    }
}

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> a >> n;
    f();
    cout << sum;
    return 0;
}